/**
 * Created by dqw on 2015/12/30.
 */

function tabCommo(id){
    $.ajax({
        type: 'POST',
        url: ncGlobal.adminRoot + "risk/json/detail",
        dataType: "json",
        data: {id:id},
        success: function (data) {
            if (200 == data.code) {
                var html = '';
                var into = "";
                for (i = 0; i < data.data.length; i++) {
                    var button = '';
                    var button1 = '';
                    if ($("#riskEdit") && $("#riskEdit").val() == 1) {
                        button = '<button class="btn btn-default btn-s amend-btn">修改</button>'
                    }
                    if ($("#riskDescEdit") && $("#riskDescEdit").val() == 1) {
                        button1 = '<button class="btn btn-default btn-s desc-btn">修改</button>'
                    }
                    into +=
                        '<tr class="" data-id="' + data.data[i].id + '" >\n' +
                        '<td class="lable ta w25">' + data.data[i].name + '</td>\n' +
                        '<td class="lable ta w30"><span class="base-info isTrue">' + data.data[i].value + '</span>' + button + '</td>\n' +
                        '<td class="lable ta w45"><span class="base-info isTrue">' + data.data[i].descript + '</span>' + button1 +'</td>\n' +
                        '</tr>'
                }
                html = '<table border="1" cellspacing="0" cellpadding="0" style="border-collapse: collapse;" class="mytable  dt-grid table table-bordered table-hover table-responsive">\n' +
                    '                                <tbody>\n' +
                    into +
                    '                                </tbody>\n' +
                    '                            </table>'


                $("#riskContiner").html(html);

                $('.amend-btn').on('click', function () {
                    var base, str;
                    if ($(this).html() == '修改') {
                        $(this).html('保存');
                        base = $(this).parents('td').find('.base-info').html();
                        str = '<input type="text" class="value">'
                        $(this).parents('td').find('.base-info').html(str);
                        $(this).parents('td').find('.value').val(base);
                    } else {
                        $(this).html('修改');
                        id = $(this).parents('tr').attr('data-id');

                        base = $(this).parents('td').find('.base-info input').val();
                        $(this).parents('td').find('.base-info').html(base);
                        $.ajax({
                            type: 'POST',
                            url: ncGlobal.adminRoot + "risk/json/edit",
                            dataType: "json",
                            data: {
                                id: id,
                                value: base,
                                type: 1
                            },
                            success: function (data) {
                                console.log(data);
                            }
                        })
                    }
                    ;
                });

                $('.desc-btn').on('click', function () {
                    var base, str;
                    if ($(this).html() == '修改') {
                        $(this).html('保存');
                        base = $(this).parents('td').find('.base-info').html();
                        str = '<input type="text" class="value">'
                        $(this).parents('td').find('.base-info').html(str);
                        $(this).parents('td').find('.value').val(base);

                    } else {
                        $(this).html('修改');
                        id = $(this).parents('tr').attr('data-id');
                        base = $(this).parents('td').find('.base-info input').val();
                        $(this).parents('td').find('.base-info').html(base);
                        $.ajax({
                            type: 'POST',
                            url: ncGlobal.adminRoot + "risk/json/edit",
                            dataType: "json",
                            data: {
                                id: id,
                                value: base,
                                type: 2
                            },
                            success: function (data) {
                                console.log(data);
                            }
                        })
                    }
                    ;
                });
            } else {
                $error.find('strong').html(req.message ? req.message : "获取数据失败")
            }
        }
    });
}

tabCommo(1);
// //操作处理开始
// var RiskHandle = function () {
//
//     function _bindEvent() {
//
//     }
//
//
//     //外部可调用
//     return {
//         bindEvent: _bindEvent,
//         tabCommo: _tabCommo
//     }
// }();
// //操作处理结束
//
// $(function () {
//     //页面绑定事件
//     RiskHandle.bindEvent();
// });